package com.cct.digitalarchapi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.cct.digitalarchapi.common.CommonUtil;
import com.cct.digitalarchapi.common.FileUtil;
import com.cct.digitalarchapi.common.Response;
import com.cct.digitalarchapi.pojo.DigitalArchMain;
import com.cct.digitalarchapi.service.ArchDeptCategoryService;
import com.cct.digitalarchapi.service.ArchDeptService;
import com.cct.digitalarchapi.service.DigitalArchMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @FileName ThirdPartyApiController
 * @Description 对外的接口
 * @Author Created by chenmingzhe
 * @Date 2023/12/20
 **/

@RestController
@RequestMapping(value = "/importData")
public class ThirdPartyApiController {

    @Autowired
    private FileUtil fileUtil;
    @Autowired
    private DigitalArchMainService digitalArchMainService;
    @Autowired
    private ArchDeptService archDeptService;
    @Autowired
    private ArchDeptCategoryService archDeptCategoryService;
    @Autowired
    private CommonUtil commonUtil;
    /**
     *数字档案导入接口
     */
    @RequestMapping(value="/importArchMain",method= RequestMethod.POST)
    public Response importArchMain(@RequestParam MultipartFile[] files, @RequestParam String archTitle, @RequestParam  int archPeriod, @RequestParam String notes,
                                   @RequestParam String archCateCode, @RequestParam  String archDeptCode, @RequestParam String year,@RequestParam String sect) {
        DigitalArchMain digitalArchMain=new DigitalArchMain();
        digitalArchMain.setArchTitle(archTitle);
        digitalArchMain.setArchPeriod(archPeriod);
        digitalArchMain.setNotes(notes);
        digitalArchMain.setArchCateCode(archCateCode);
        digitalArchMain.setArchDeptCode(archDeptCode);
        String archMainCode=digitalArchMainService.addImportArchMain(digitalArchMain,year,sect);
        for (MultipartFile file : files) {
            fileUtil.uploadFile(file,archMainCode);
        }
        commonUtil.recordOperateLog("第三方平台导入了档案名称为"+archTitle+"的档案");
        return Response.success("");
    }

    /**
     *查询所有部门
     */
    @RequestMapping(value="/getAllDept",method= RequestMethod.POST)
    public Response getAllDept(){
        return archDeptService.getAllDept();
    }
    /**
    *根据部门查档案分类
    */
    @RequestMapping(value="/getCateByDept",method= RequestMethod.POST)
    public Response getCateByDept(@RequestBody JSONObject obj) {
        String archDeptName=obj.getString("archDeptName");
        return archDeptCategoryService.getCateByDept(archDeptName);
    }

    /**
     *获取宗号
     */
    @RequestMapping(value="/getSectList",method= RequestMethod.POST)
    public Response getSectList(@RequestBody JSONObject obj) {
        String archDeptCode=obj.getString("archDeptCode");
        String archCateCode=obj.getString("archCateCode");
        return digitalArchMainService.getSectList(archDeptCode,archCateCode);
    }
}
